/**
  ******************************************************************************
  * @file    Eeprom.c.c  
  * @author  Dajin Li
  * @version V0.0.1
  * @date    08-08-2015 
  * @brief   This file contains IIC read/write EEPROM  function
  *          
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; KION(SZ) R&D Center
  *
  * Licensed under KION, (the "License");
  *
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/

#include "stm32f10x.h"
#include "string.h"
#include "global.h"
#include "spi.h"
#include "CAN_canopen.h"
#include "var_tbl.h"
#include "menu.h"
#include "hourmeter.h"
//#include "CAN1_J1939.h"
#include "lcd.h"


void Truck_based_param(void)
{
    if (INTER_TRUCK_TYPE == T1347)
    {
        truck_id = BR1347_ID;
        truck_type = BR1347;
        kwp_data_params[VAR_DISPLAY_OPTIONS_INDEX].default_value |= TRAVEL_SPD_DISP_BIT | SOC_DISPLAY_BIT;
        kwp_data_params[VAR_TECH_PWD_INDEX].default_value = 0x1347;
        kwp_data_params[VAR_SER_PWD_INDEX].default_value = 0x1347;
        kwp_data_params[VAR_UNITS_INDEX].default_value = 1;
        kwp_data_params[VAR_DATE_FORMAT_INDEX].default_value = 2;
    }
    else if (INTER_TRUCK_TYPE == T1275LI)
    {
        truck_id = BR1275LI_ID;
        truck_type = BR1347;
        kwp_data_params[VAR_DISPLAY_OPTIONS_INDEX].default_value |= TRAVEL_SPD_DISP_BIT | SOC_DISPLAY_BIT;
        kwp_data_params[VAR_TECH_PWD_INDEX].default_value = 0x1275;
        kwp_data_params[VAR_SER_PWD_INDEX].default_value = 0x1275;
    }
    else if (INTER_TRUCK_TYPE == T1275)
    {
        truck_id = BR1275_ID;
        truck_type = BR1347;
        kwp_data_params[VAR_DISPLAY_OPTIONS_INDEX].default_value |= TRAVEL_SPD_DISP_BIT | SOC_DISPLAY_BIT;
        kwp_data_params[VAR_TECH_PWD_INDEX].default_value = 0x1275;
        kwp_data_params[VAR_SER_PWD_INDEX].default_value = 0x1275;
    }
    else if (INTER_TRUCK_TYPE == T1276)
    {
        truck_id = BR1276_ID;
        truck_type = BR1347;
        kwp_data_params[VAR_DISPLAY_OPTIONS_INDEX].default_value |= TRAVEL_SPD_DISP_BIT | SOC_DISPLAY_BIT;
        kwp_data_params[VAR_TECH_PWD_INDEX].default_value = 0x1276;
        kwp_data_params[VAR_SER_PWD_INDEX].default_value = 0x1276;
    }
    else if (INTER_TRUCK_TYPE == T1276LI)
    {
        truck_id = BR1276LI_ID;
        truck_type = BR1347;
        kwp_data_params[VAR_DISPLAY_OPTIONS_INDEX].default_value |= TRAVEL_SPD_DISP_BIT | SOC_DISPLAY_BIT;
        kwp_data_params[VAR_TECH_PWD_INDEX].default_value = 0x1276;
        kwp_data_params[VAR_SER_PWD_INDEX].default_value = 0x1276;
    }
    
}


/**
* @brief    reads param from EEPROM.
* @param    none
* @note     none
* @retval   none
*/
void load_parameters_from_ee(void)
{
    s16 i;
    u8 *char_ptr;
    u16 ee_addr;
    u8 length;
    u16 var_attr;
    u16 value;

    EE_SPI_ReadBuffer((u8 *)(&value), 8190, 2);

    if(value != 0xAA55)
    {  
//        test_pattern(0xff);

        /* If read ee fail the first time, delay 1s, waiting  for spi stable, and then read once again.*/
        IWDG_ReloadCounter();
        osDelay(1000);
        IWDG_ReloadCounter();
        EE_SPI_ReadBuffer((u8 *)(&value), 8190, 2);
    }

    if( value != 0xAA55)   // first startup
    {
        EE_SPI_Write_Fixed_Data(0x00, 200, 7992);  // clear all
        value = 0xAA55;
        EE_SPI_WriteBuffer((u8 *)(&value), 8190, 2);
        Truck_based_param();    
        for( i = 0; i < MAX_KWP_DATA_OBJECT_DEF; i++)
        {
            var_attr = kwp_data_params[i].attr;
            if( var_attr & SAVE_TO_EE )
            {
                length = var_attr & VAR_LENGTH_MASK;
                if(length <= 4)
                {
                    char_ptr = (u8 *) kwp_data_params[i].var;

                    if(length == 1)
                    {
                        *char_ptr = kwp_data_params[i].default_value;
                    }
                    else if(length == 2)
                    {
                        *(u16 *)char_ptr = kwp_data_params[i].default_value;
                    }     
                    else
                    {
                        *(u32 *)char_ptr = kwp_data_params[i].default_value;
                    }
                    ee_addr = kwp_data_params[i].ee_addr;
                    EE_SPI_WriteBuffer(char_ptr, ee_addr, length);
                }
            }
        }
    }
    else
    {
        Truck_based_param();   
       
        for( i = 0; i < MAX_KWP_DATA_OBJECT_DEF; i++)
        {
            var_attr = kwp_data_params[i].attr;
            if(var_attr & SAVE_TO_EE)
            {
                char_ptr = (u8 *) kwp_data_params[i].var;
                ee_addr = kwp_data_params[i].ee_addr;
                length = var_attr & VAR_LENGTH_MASK;
                EE_SPI_ReadBuffer(char_ptr, ee_addr, length);
            }
        }
    }
    ParaInit();
}
/**
* @brief    save param to EEPROM.
* @param    none
* @note     none
* @retval   none
*/
void Save_Parameter(void)
{   
    u8 *char_ptr;
    u16 var_attr;
    u16 ee_addr;
    u8 length;
    s16 i;

    if(kwp_needs_ee)
    {
        kwp_needs_ee = 0;
        char_ptr = (u8 *) kwp_data_params[kwp_ee_index].var;
        ee_addr = kwp_data_params[kwp_ee_index].ee_addr;
        length = kwp_data_params[kwp_ee_index].attr & VAR_LENGTH_MASK;
        EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 


        if(kwp_ee_index == VAR_PART_NUMBER_INDEX)
        {
            if(part_number[0] != 0x30 || part_number[1] != 0x30 || part_number[2] != 0x30 || part_number[3] != 0x30 || part_number[4] != 0x30 || part_number[5] != 0x30 || part_number[6] != 0x30 ||
               part_number[7] != 0x30 || part_number[8] != 0x30 || part_number[9] != 0x30 || part_number[10] != 0x30)
            {
                for( i = 0; i < 11; i++)
                {
                    last_part_number[i] = part_number[i];
                }
                kwp_ee_index = VAR_LAST_PARTNUMBER_INDEX;
                char_ptr = (u8 *) kwp_data_params[kwp_ee_index].var;
                ee_addr = kwp_data_params[kwp_ee_index].ee_addr;
                length = kwp_data_params[kwp_ee_index].attr & VAR_LENGTH_MASK;
                EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 
            }
        }

        if(kwp_ee_index == VAR_PIN_EN_INDEX || kwp_ee_index == VAR_DISPLAY_OPTIONS_INDEX)
        {
            if(((display_options & PIN_CODE_EN_BIT) == 0) || (pin_enable == 0))
            {
                fault_status &= ~PIN_CODE_FAULT;
            }
        }
    }

    else if(menu_needs_ee)
    {
        menu_needs_ee = 0;
        char_ptr = (u8 *) kwp_data_params[menu_ee_index].var;
        ee_addr = kwp_data_params[menu_ee_index].ee_addr;
        length = kwp_data_params[menu_ee_index].attr & VAR_LENGTH_MASK;
        EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 


        if(kwp_ee_index == VAR_PIN_EN_INDEX || kwp_ee_index == VAR_DISPLAY_OPTIONS_INDEX)
        {
            if(((display_options & PIN_CODE_EN_BIT) == 0) || (pin_enable == 0))
            {
                fault_status &= ~PIN_CODE_FAULT;
            }
        }

        if(menu_ee_index == VAR_UNITS_INDEX)
        {
            if(speedometer_units == 0)
            {
                safety_speed_value = (s32)safety_speed_value * MPH_TO_KM;
                if(safety_speed_value < MIN_TRACTION_SPD_KMH)
                {
                    safety_speed_value = MIN_TRACTION_SPD_KMH;
                }
                else if(safety_speed_value > MAX_TRACTION_SPD_KMH)
                {
                    safety_speed_value = MAX_TRACTION_SPD_KMH;
                } 
            }
            else
            {
                safety_speed_value = (s32)safety_speed_value * KM_TO_MPH;
                if(safety_speed_value < MIN_TRACTION_SPD_MPH)
                {
                    safety_speed_value = MIN_TRACTION_SPD_MPH;
                }
                else if(safety_speed_value > MAX_TRACTION_SPD_MPH)
                {
                    safety_speed_value = MAX_TRACTION_SPD_MPH;
                } 
                
            }

            safety_speed_data = safety_speed_value;

            menu_ee_index = VAR_MAX_TRACTION_SPD_FORW_INDEX;
            char_ptr = (u8 *) kwp_data_params[menu_ee_index].var;
            ee_addr = kwp_data_params[menu_ee_index].ee_addr;
            length = kwp_data_params[menu_ee_index].attr & VAR_LENGTH_MASK;
            EE_SPI_WriteBuffer(char_ptr, ee_addr, length);

            menu_ee_index = VAR_MAX_TRACTION_SPD_BACK_INDEX;
            char_ptr = (u8 *) kwp_data_params[menu_ee_index].var;
            ee_addr = kwp_data_params[menu_ee_index].ee_addr;
            length = kwp_data_params[menu_ee_index].attr & VAR_LENGTH_MASK;
            EE_SPI_WriteBuffer(char_ptr, ee_addr, length);

            menu_ee_index = VAR_SAFETY_SPD_INDEX;
            char_ptr = (u8 *) kwp_data_params[menu_ee_index].var;
            ee_addr = kwp_data_params[menu_ee_index].ee_addr;
            length = kwp_data_params[menu_ee_index].attr & VAR_LENGTH_MASK;
            EE_SPI_WriteBuffer(char_ptr, ee_addr, length);
            
        }
    }
    else if (pre_DigicodeArray1Value != DigicodeArray1Value)
    {
        pre_DigicodeArray1Value = DigicodeArray1Value;
        char_ptr = (u8 *) kwp_data_params[VAR_KEYON_HOURS_INDEX].var;
        ee_addr = kwp_data_params[VAR_KEYON_HOURS_INDEX].ee_addr;
        length = kwp_data_params[VAR_KEYON_HOURS_INDEX].attr & VAR_LENGTH_MASK;
        EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 
    }
    else if(Hrm_Struct.Pre_HM1_value != Hrm_Struct.HM1_value)
    {
        Hrm_Struct.Pre_HM1_value = Hrm_Struct.HM1_value;
        char_ptr = (u8 *) kwp_data_params[VAR_KEYON_HOURS_INDEX].var;
        ee_addr = kwp_data_params[VAR_KEYON_HOURS_INDEX].ee_addr;
        length = kwp_data_params[VAR_KEYON_HOURS_INDEX].attr & VAR_LENGTH_MASK;
        EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 
    }
    else if(Hrm_Struct.Pre_HM2_value != Hrm_Struct.HM2_value)
    {
        Hrm_Struct.Pre_HM2_value = Hrm_Struct.HM2_value;
        char_ptr = (u8 *) kwp_data_params[VAR_SEAT_HOURS_INDEX].var;
        ee_addr = kwp_data_params[VAR_SEAT_HOURS_INDEX].ee_addr;
        length = kwp_data_params[VAR_SEAT_HOURS_INDEX].attr & VAR_LENGTH_MASK;
        EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 
    }
    else if(Hrm_Struct.Pre_HM3_value != Hrm_Struct.HM3_value)
    {
        Hrm_Struct.Pre_HM3_value = Hrm_Struct.HM3_value;
        char_ptr = (u8 *) kwp_data_params[VAR_MOTOR_HOURS_INDEX].var;
        ee_addr = kwp_data_params[VAR_MOTOR_HOURS_INDEX].ee_addr;
        length = kwp_data_params[VAR_MOTOR_HOURS_INDEX].attr & VAR_LENGTH_MASK;
        EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 
    }
    else if(Hrm_Struct.Pre_HM4_value != Hrm_Struct.HM4_value)
    {
        Hrm_Struct.Pre_HM4_value = Hrm_Struct.HM4_value;
        char_ptr = (u8 *) kwp_data_params[VAR_STEER_HOURS_INDEX].var;
        ee_addr = kwp_data_params[VAR_STEER_HOURS_INDEX].ee_addr;
        length = kwp_data_params[VAR_STEER_HOURS_INDEX].attr & VAR_LENGTH_MASK;
        EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 
    }
    else if(Hrm_Struct.Pre_service_hours2 != Hrm_Struct.Service_hours2)
    {
        Hrm_Struct.Pre_service_hours2 = Hrm_Struct.Service_hours2;
        char_ptr = (u8 *) kwp_data_params[VAR_CHANGE_AIR_FILTER_INDEX].var;
        ee_addr = kwp_data_params[VAR_CHANGE_AIR_FILTER_INDEX].ee_addr;
        length = kwp_data_params[VAR_CHANGE_AIR_FILTER_INDEX].attr & VAR_LENGTH_MASK;
        EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 
    }
    else if(Hrm_Struct.Pre_service_hours1 != Hrm_Struct.Service_hours1)
    {
        Hrm_Struct.Pre_service_hours1 = Hrm_Struct.Service_hours1;
        char_ptr = (u8 *) kwp_data_params[VAR_CHANGE_ENG_OIL_INDEX].var;
        ee_addr = kwp_data_params[VAR_CHANGE_ENG_OIL_INDEX].ee_addr;
        length = kwp_data_params[VAR_CHANGE_ENG_OIL_INDEX].attr & VAR_LENGTH_MASK;
        EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 
    }
    else if(Hrm_Struct.Pre_service_hours3!= Hrm_Struct.Service_hours3)
    {
        Hrm_Struct.Pre_service_hours3 = Hrm_Struct.Service_hours3;
        char_ptr = (u8 *) kwp_data_params[VAR_CHANGE_SERVICE_ENG_INDEX].var;
        ee_addr = kwp_data_params[VAR_CHANGE_SERVICE_ENG_INDEX].ee_addr;
        length = kwp_data_params[VAR_CHANGE_SERVICE_ENG_INDEX].attr & VAR_LENGTH_MASK;
        EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 
    }
    else if(Hrm_Struct.Pre_service_hours4!= Hrm_Struct.Service_hours4)
    {
        Hrm_Struct.Pre_service_hours4 = Hrm_Struct.Service_hours4;
        char_ptr = (u8 *) kwp_data_params[VAR_CHANGE_SERVICE_MACHINE_INDEX].var;
        ee_addr = kwp_data_params[VAR_CHANGE_SERVICE_MACHINE_INDEX].ee_addr;
        length = kwp_data_params[VAR_CHANGE_SERVICE_MACHINE_INDEX].attr & VAR_LENGTH_MASK;
        EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 
    }
    else if(Hrm_Struct.Pre_service_days2 != Hrm_Struct.Service_days2)
    {
        Hrm_Struct.Pre_service_days2 = Hrm_Struct.Service_days2;
        char_ptr = (u8 *) kwp_data_params[VAR_CHANGE_AIR_FILTER_MONTH_INDEX].var;
        ee_addr = kwp_data_params[VAR_CHANGE_AIR_FILTER_MONTH_INDEX].ee_addr;
        length = kwp_data_params[VAR_CHANGE_AIR_FILTER_MONTH_INDEX].attr & VAR_LENGTH_MASK;
        EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 
    }
    else if(Hrm_Struct.Pre_service_days1 != Hrm_Struct.Service_days1 )
    {
        Hrm_Struct.Pre_service_days1 = Hrm_Struct.Service_days1;
        char_ptr = (u8 *) kwp_data_params[VAR_CHANGE_ENG_OIL_MONTH_INDEX].var;
        ee_addr = kwp_data_params[VAR_CHANGE_ENG_OIL_MONTH_INDEX].ee_addr;
        length = kwp_data_params[VAR_CHANGE_ENG_OIL_MONTH_INDEX].attr & VAR_LENGTH_MASK;
        EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 
    }
    else if(Hrm_Struct.Pre_service_days3 != Hrm_Struct.Service_days3)
    {
        Hrm_Struct.Pre_service_days3 = Hrm_Struct.Service_days3;
        char_ptr = (u8 *) kwp_data_params[VAR_SER_ENG_MONTH_INDEX].var;
        ee_addr = kwp_data_params[VAR_SER_ENG_MONTH_INDEX].ee_addr;
        length = kwp_data_params[VAR_SER_ENG_MONTH_INDEX].attr & VAR_LENGTH_MASK;
        EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 
    }
    else if(Hrm_Struct.Pre_service_days4 != Hrm_Struct.Service_days4 )
    {
        Hrm_Struct.Pre_service_days4 = Hrm_Struct.Service_days4;
        char_ptr = (u8 *) kwp_data_params[VAR_SER_MACHINE_MONTH_INDEX].var;
        ee_addr = kwp_data_params[VAR_SER_MACHINE_MONTH_INDEX].ee_addr;
        length = kwp_data_params[VAR_SER_MACHINE_MONTH_INDEX].attr & VAR_LENGTH_MASK;
        EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 
    }

    if(error_reset == VAR_STORED_FAULT0_INDEX)
    {
        error_reset = 0;
        for(i = 0; i < MAX_STORED_FAULT; i++)
        {
            memset((u8 *)(&display_stored_fault[i]), 0, sizeof(E_STORED_FAULT_PARAM));
            display_stored_fault[i].save_flag = 1;
            display_total_stored_fault = 0;
        }
    }    

    for(i = 0; i < MAX_STORED_FAULT; i++)
    {
        if(display_stored_fault[i].save_flag == 1)
        {
            display_stored_fault[i].save_flag = 0;
            char_ptr = (u8 *)(&display_stored_fault[i]);
            ee_addr = kwp_data_params[VAR_STORED_FAULT0_INDEX+ i].ee_addr;
            length = kwp_data_params[VAR_STORED_FAULT0_INDEX + i].attr & VAR_LENGTH_MASK;
            EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 
            break;
        }
    }

    if(truck_type == BR1347)
    {
        if(error_reset == VAR_STORED_FAULT2_INDEX)
        {
            error_reset = 0;
            for(i = 0; i < MAX_STORED_FAULT; i++)
            {
                memset((char *)(&br1347_traction_stored_fault[i]), 0, 16);
                br1347_traction_stored_fault[i].save_flag = 1;
                br1347_trac_total_stored_fault = 0;

            }
        }    

        for(i = 0; i < MAX_STORED_FAULT; i++)
        {
            if(br1347_traction_stored_fault[i].save_flag == 1)
            {
                br1347_traction_stored_fault[i].save_flag = 0;
                char_ptr = (u8 *)(&br1347_traction_stored_fault[i]);
                ee_addr = kwp_data_params[VAR_STORED_FAULT2_INDEX + i].ee_addr;
                length = kwp_data_params[VAR_STORED_FAULT2_INDEX + i].attr & VAR_LENGTH_MASK;
                EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 
                break;
            }
        }
        
        if(error_reset == VAR_STORED_FAULT1_INDEX)
        {
            error_reset = 0;
            for(i = 0; i < MAX_STORED_FAULT; i++)
            {
                memset((char *)(&br1347_lift_stored_fault[i]), 0, 16);
                br1347_lift_stored_fault[i].save_flag = 1;
                br1347_lift_total_stored_fault = 0;
            }
        } 

        for(i = 0; i < MAX_STORED_FAULT; i++)
        {
            if(br1347_lift_stored_fault[i].save_flag == 1)
            {
                br1347_lift_stored_fault[i].save_flag = 0;
                char_ptr = (u8 *)(&br1347_lift_stored_fault[i]);
                ee_addr = kwp_data_params[VAR_STORED_FAULT1_INDEX + i].ee_addr;
                length = kwp_data_params[VAR_STORED_FAULT1_INDEX + i].attr & VAR_LENGTH_MASK;
                EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 
                break;
            }
        }

        // reset all of the bms buffer to zero if reset stored errors enable
        if(error_reset == VAR_STORED_FAULT3_INDEX)
        {
            error_reset = 0;
            for(i = 0; i < MAX_STORED_FAULT; i++)
            {
                memset((char *)(&br1275_bms_stored_fault[i]), 0, 16);
                br1275_bms_stored_fault[i].save_flag = 1;
                br1275_bms_total_stored_fault = 0;
            }
        } 

        // save all of the bms stored errors code to eeprom
        for(i = 0; i < MAX_STORED_FAULT; i++)
        {
            if(br1275_bms_stored_fault[i].save_flag == 1)
            {
                br1275_bms_stored_fault[i].save_flag = 0;
                char_ptr = (u8 *)(&br1275_bms_stored_fault[i]);
                ee_addr = kwp_data_params[VAR_STORED_FAULT3_INDEX + i].ee_addr;
                length = kwp_data_params[VAR_STORED_FAULT3_INDEX + i].attr & VAR_LENGTH_MASK;
                EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 
                break;
            }
        }
    }
    
    
    if(default_reset)
    {
        if((default_reset & DEF_RST_TYPE_FLAG) == 0x1111 || (default_reset & DEF_RST_TYPE_FLAG) == 0x3333)
        {
            EE_SPI_Write_Fixed_Data(0x00, 200, 7990);

            if(INTER_TRUCK_TYPE == T1347)
            {
                kwp_data_params[VAR_TECH_PWD_INDEX].default_value = 0x1347;
                kwp_data_params[VAR_SER_PWD_INDEX].default_value = 0x1347;
                kwp_data_params[VAR_UNITS_INDEX].default_value = 1;
                kwp_data_params[VAR_DATE_FORMAT_INDEX].default_value = 2;
                kwp_data_params[VAR_DISPLAY_OPTIONS_INDEX].default_value |= TRAVEL_SPD_DISP_BIT | SOC_DISPLAY_BIT;
            }
            else if(INTER_TRUCK_TYPE == T1276)
            {
                kwp_data_params[VAR_TECH_PWD_INDEX].default_value = 0x1276;
                kwp_data_params[VAR_SER_PWD_INDEX].default_value = 0x1276;
                kwp_data_params[VAR_DISPLAY_OPTIONS_INDEX].default_value |= TRAVEL_SPD_DISP_BIT | SOC_DISPLAY_BIT;
            }
            else if(INTER_TRUCK_TYPE == T1275)
            {
                kwp_data_params[VAR_TECH_PWD_INDEX].default_value = 0x1275;
                kwp_data_params[VAR_SER_PWD_INDEX].default_value = 0x1275;
                kwp_data_params[VAR_DISPLAY_OPTIONS_INDEX].default_value |= TRAVEL_SPD_DISP_BIT | SOC_DISPLAY_BIT;
            }
            else if (INTER_TRUCK_TYPE == T1275LI)
            {
                kwp_data_params[VAR_TECH_PWD_INDEX].default_value = 0x1275;
                kwp_data_params[VAR_SER_PWD_INDEX].default_value = 0x1275;
                kwp_data_params[VAR_DISPLAY_OPTIONS_INDEX].default_value |= TRAVEL_SPD_DISP_BIT | SOC_DISPLAY_BIT;
            }
            else if (INTER_TRUCK_TYPE == T1276LI)
            {
                kwp_data_params[VAR_TECH_PWD_INDEX].default_value = 0x1276;
                kwp_data_params[VAR_SER_PWD_INDEX].default_value = 0x1276;
                kwp_data_params[VAR_DISPLAY_OPTIONS_INDEX].default_value |= TRAVEL_SPD_DISP_BIT | SOC_DISPLAY_BIT;
            }            
            
            for( i = 0; i < MAX_KWP_DATA_OBJECT_DEF; i++)
            {
                var_attr = kwp_data_params[i].attr;
                if( var_attr & SAVE_TO_EE )
                {
                    length = var_attr & VAR_LENGTH_MASK;
                    ee_addr = kwp_data_params[i].ee_addr;
                    if((length <= 4) && (ee_addr >= 200))
                    {
                        char_ptr = (u8 *) kwp_data_params[i].var;

                        if(length == 1)
                        {
                            *char_ptr = kwp_data_params[i].default_value;
                        }
                        else if(length == 2)
                        {
                            *(u16 *)char_ptr = kwp_data_params[i].default_value;
                        }     
                        else
                        {
                            *(u32 *)char_ptr = kwp_data_params[i].default_value;
                        }
                        EE_SPI_WriteBuffer(char_ptr, ee_addr, length);
                    }
                }
            }
        }
        else if((default_reset & DEF_RST_TYPE_FLAG) == 0xAFAF)
        {
            EE_SPI_Write_Fixed_Data(0x00, 200, 7990);

            Truck_based_param();
            for( i = 0; i < MAX_KWP_DATA_OBJECT_DEF; i++)
            {
                var_attr = kwp_data_params[i].attr;
                if( var_attr & SAVE_TO_EE )
                {
                    length = var_attr & VAR_LENGTH_MASK;
                    ee_addr = kwp_data_params[i].ee_addr;
                    if(length <= 4)
                    {
                        char_ptr = (u8 *) kwp_data_params[i].var;

                        if(length == 1)
                        {
                            *char_ptr = kwp_data_params[i].default_value;
                        }
                        else if(length == 2)
                        {
                            *(u16 *)char_ptr = kwp_data_params[i].default_value;
                        }     
                        else
                        {
                            *(u32 *)char_ptr = kwp_data_params[i].default_value;
                        }
                        EE_SPI_WriteBuffer(char_ptr, ee_addr, length);
                    }
                }
            }
        }

        
        if((default_reset & DEF_RST_TYPE_FLAG) == 0x2222 || (default_reset & DEF_RST_TYPE_FLAG) == 0x3333)  // reset all the hourmeters
        {
            Hrm_Struct.HM1_value = ((default_reset & DEF_INIT_KEYON_VAL_FLAG) >> 16) & 0xff;
            Hrm_Struct.Pre_HM1_value = Hrm_Struct.HM1_value;
            char_ptr = (u8 *) kwp_data_params[VAR_KEYON_HOURS_INDEX].var;
            ee_addr = kwp_data_params[VAR_KEYON_HOURS_INDEX].ee_addr;
            length = kwp_data_params[VAR_KEYON_HOURS_INDEX].attr & VAR_LENGTH_MASK;
            EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 

            Hrm_Struct.HM2_value = ((default_reset & DEF_INIT_SEATON_VAL_FLAG) >> 24) & 0xff;
            Hrm_Struct.Pre_HM2_value = Hrm_Struct.HM2_value;
            char_ptr = (u8 *) kwp_data_params[VAR_SEAT_HOURS_INDEX].var;
            ee_addr = kwp_data_params[VAR_SEAT_HOURS_INDEX].ee_addr;
            length = kwp_data_params[VAR_SEAT_HOURS_INDEX].attr & VAR_LENGTH_MASK;
            EE_SPI_WriteBuffer(char_ptr, ee_addr, length); 

            Hrm_Struct.HM3_value = 0;
            Hrm_Struct.Pre_HM3_value = Hrm_Struct.HM3_value;
            char_ptr = (u8 *) kwp_data_params[VAR_MOTOR_HOURS_INDEX].var;
            ee_addr = kwp_data_params[VAR_MOTOR_HOURS_INDEX].ee_addr;
            length = kwp_data_params[VAR_MOTOR_HOURS_INDEX].attr & VAR_LENGTH_MASK;
            EE_SPI_WriteBuffer(char_ptr, ee_addr, length);
        }
        default_reset = 0;

        load_parameters_from_ee();
    }
}
/**
* @brief    save parameter task
* @param    none
* @note     none
* @retval   none
*/
void eeprom_task(void const *argument) 
{
    // Initialization 
    task_status[SPI_EEPROM_ID] = ALIVE;
    // Tasks must run in an infinite loop:
    while(TRUE) 
    {   
        // ...place code for sample frequency inputs here...        
        Save_Parameter();
        task_status[SPI_EEPROM_ID] = ALIVE;
        osDelay(SPI_EEPROM_ITV);
    }  // end while infinite loop
}  // end task_sample_freq()  

